#include <config.h>
#include <utils.h>
#include <os.h>
#include <device.h>
#include <printk.h>
#include <settings.h>
#include <syserr.h>
#include "board.h"
#include "mbus_smart.h"
#include "encode.h"
#include "dev.h"
#include "dev_show.h"
#include "auto_report.h"
//#include "control.h"
#include "smart_sensor.h"
#include "sensor_test.h"

static const uint8_t dev_type[8] = {0xFF,0xFF,0x37,0x00,0x05,0x00,0x00,0x00};//新光照度传感器类型
static const char soft_ver[] = "P-SN-IM-MBus(v1.0)-20190619";
static tcb_t app_tcb;
device_t *dev_led;

void dev_show(void)
{
    static char str[0x20];
    dev_ver_get(str, sizeof(str));
    log_d("VER: %s\n", 	str, sizeof(str));
    log_d("AID: %s\n", 	arr2str(setting.encode.id, 	 sizeof(setting.encode.id),  str, sizeof(str)));
    log_d("PWD: %s\n", 	arr2str(setting.encode.pwd,  sizeof(setting.encode.pwd), str, sizeof(str)));
    log_d("SN: %s\n",  	arr2str(setting.encode.sn, 	 sizeof(setting.encode.sn),  str, sizeof(str)));
    log_d("DKey: %s\n", arr2str(setting.encode.dkey, sizeof(setting.encode.dkey),str, sizeof(str)));
    log_d("PID: %s\n",  arr2str(setting.para.panid,  sizeof(setting.para.panid), str, sizeof(str)));
    log_d("GID: %s\n",  arr2str(setting.para.gid,    sizeof(setting.para.gid),   str, sizeof(str)));
    log_d("SID: %s\n",  arr2str(setting.para.sid,    sizeof(setting.para.sid),   str, sizeof(str)));
}

int dev_type_get(void *out, int maxlen)
{
    memcpy(out, dev_type, sizeof(dev_type));
    return sizeof(dev_type);
}

int dev_type_cmp(const void *dt, size_t len)
{
    return len == sizeof(dev_type) && !memcmp(dt, dev_type, sizeof(dev_type));
}

int dev_ver_get(void *out, int maxlen)
{
    int len = strlen(soft_ver);

    memcpy(out, soft_ver, len);
    return len;
}

int dev_ver_cmp(const void *ver, size_t len)
{
    char softver[0x20];

    int verlen = dev_ver_get(softver, sizeof(softver));

    return len == verlen && !memcmp(ver, softver, verlen);
}

//mycard取自SN:年+周+流水号  搜表时匹配mycard
int dev_mycard_get(uint8_t mycard[])
{
	mycard[0] = setting.encode.sn[6];
	mycard[1] = setting.encode.sn[7];
	mycard[2] = setting.encode.sn[9];
	mycard[3] = setting.encode.sn[10];
	mycard[4] = setting.encode.sn[11];
	
	return 1;
}	

static void setup_led(void)
{
    dev_led = device_find("led");
    assert(dev_led);
    device_open(dev_led, 0);
}

/*
* sec
*/
static void sec_tmr_cb(struct soft_timer *st)
{
    st->expires += configHZ;
    soft_timer_add(st);
	
//#ifndef VM_DEBUG	
//	smart_sensor_read_hook_1s();
//#endif
	smart_sensor_read_hook_1s();
	device_ctrl(dev_led,STATE_LED_FLASH_3,NULL);
}

static struct soft_timer st =
{
    .cb= sec_tmr_cb,
};
static int sec_tmr_init(void)
{
    st.expires = jiffies + configHZ;
    soft_timer_add(&st);
    return 0;
}
/*
* 100ms timer
*/
static void ms100_tmr_cb(struct soft_timer *st)
{
    st->expires += 10;
    soft_timer_add(st);
	
#ifdef VM_DEBUG
	smart_sensor_read_hook_1s();
#endif
}

static struct soft_timer mst =
{
     .cb = ms100_tmr_cb,
};
static int ms100_tmr_init(void)
{
    mst.expires = jiffies + 10;
    soft_timer_add(&mst);
    return 0;
}

static void app_task_cb(struct task_ctrl_blk *tcb, ubase_t data)
{
    tSTART(tcb);
	
	ms100_tmr_init();
    sec_tmr_init();
	setup_led();
	setup_ntc();
	setup_vl();
	setup_ir();
	setup_veml6030();
	
    for (;;)
    {
        task_wait_signal(tcb);
        sig_t sig = task_signal(tcb);
        //if (sigget(sig, SIG_KEY));
			// do_key();
    }
    tEND();
}

void setup_app(void)
{
    //setting_load();       //内部参数初始化
    //device_encode_opt();  //SN encode
    //ms100_tmr_init();
    sec_tmr_init();
    //dev_show();           //设备指示
   //veml6030_init("i2c1");
   // mbus_init();					//创建mbus任务

   // task_create(&app_tcb, app_task_cb, 0);
//	auto_report_init(get_le_val(setting.dev_infor.power_on_delay_time,2), setting.para.gid,setting.encode.id,setting.para.sid, &setting.dev_infor.report, &setting.dev_infor.dev_mode);
//	local_control_init(setting.encode.id,setting.local_ctrl.valid_id,setting.local_ctrl.gid_num,setting.local_ctrl.cnt,setting.local_ctrl.t_id,&setting.dev_infor.dev_mode);
}
